<?php

namespace Common;

use Common\Model\UserHospitalModel;
use Think\Controller;
use Common\Model\UserCarModel;

Class CommonController extends Controller {

    public $type;    // car or hospital
    public $userCar;    //120端用户模型
    public $userHospital;

    public function __construct() {
        parent::__construct();

        //检查token & 保存token&用户信息
        $this->checkToken();
    }

    private function checkToken() {
        $token = I('param.token');
        if (empty($token)) {
            $this->ajaxReturn(['status' => 401, 'error_info' => '用户未登陆，请登陆']);
        }

        $userCarModel = new UserCarModel;
        $res = $userCarModel->where(['token' => $token])->find();
        if (emptY($res)) {
            $userHospitalModel = new UserHospitalModel();
            $res = $userHospitalModel->where(['token' => $token])->find();

            if (empty($res)) {
                $this->ajaxReturn(['status' => 0, 'error_info' => '用户登陆信息不正确，请重新登陆']);
            } else {
                $this->type = 'hospital';
            }
        } else {
            $this->type = 'car';
        }

        if ($res) {
            $this->userCar = $userCarModel;
        } else {
            $this->ajaxReturn(['status' => 401, 'error_info' => '用户登陆信息不正确，请重新登陆']);
        }
    }

}

